package org.adorsys.aderp.cash.work.cashstate;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.adorsys.aderp.cash.domain.CashRegister;
import org.springframework.util.Assert;
/**
 * an utility class which help to setting my own FullDayCashRegister's specifics
 * variables. I use this class after a call of basicCashRegStateMainter.
 * ie :
 * @Resource 
 * private CashRegStateMaintainer cashRegStateMaintainer;
 * .....
 * public void openCashRegister(CashRegister c){
 * BasicFullDayCashRegister bFCashReg = new 
 * cashRegStateMaintainer.open(fullCashRegister
 * }
 * 
 * @author adorsys
 *
 */
public class BasicCashRegStateMaintainerUtil{
	/**
	 * set the date and the note of the maintaining operation
	 * @param dayCashRegister
	 */
	public static void openFullDayCash(BasicFullDayCashRegister dayCashRegister){
		dayCashRegister.setLastStateModifDate(new Date());
		dayCashRegister.setLog("opened");
	}
	public static void closeFullDayCash(BasicFullDayCashRegister dayCashRegister){
		dayCashRegister.setLastStateModifDate(new Date());
		dayCashRegister.setLog("closed");
	}
	public static void suspendFullDayCash(BasicFullDayCashRegister dayCashRegister){
		dayCashRegister.setLastStateModifDate(new Date());
		dayCashRegister.setLog("suspended");
	}
	/**
	 * return opened cash register
	 * @param fullDayCashRegisters 
	 * @return 
	 */
	public static List<CashRegister> getOpenedCash(List<FullDayCashRegister> fullDayCashRegisters){
		List<CashRegister> openedCash = new LinkedList<CashRegister>();
		for(FullDayCashRegister fullDayCashReg : fullDayCashRegisters){
			if(CashRegisterState.OPEN.equals(fullDayCashReg.getCashRegisterState())){
				openedCash.add(fullDayCashReg.getCashRegister());
			}
		}
		return openedCash;
	}
	/**
	 * return suspended cashregister
	 * @return
	 */
	public static List<CashRegister> getSuspendedCashRegister(List<FullDayCashRegister> fullDayCashRegisters){
		List<CashRegister> suspendedCash = new LinkedList<CashRegister>();
		for(FullDayCashRegister fullDayCashReg : fullDayCashRegisters){
			if(CashRegisterState.SUSPENDED.equals(fullDayCashReg.getCashRegisterState())){
				suspendedCash.add(fullDayCashReg.getCashRegister());
			}
		}
		return suspendedCash;
	}
	/**
	 * return closed cashRegister
	 * @return
	 */
	public static List<CashRegister> getClosedCashRegister(List<FullDayCashRegister> fullDayCashRegisters){
		List<CashRegister> closedCash = new LinkedList<CashRegister>();
		for(FullDayCashRegister fullDayCashReg : fullDayCashRegisters){
			if(CashRegisterState.CLOSED.equals(fullDayCashReg.getCashRegisterState())){
				closedCash.add(fullDayCashReg.getCashRegister());
			}
		}
		return closedCash;
	}
	public static FullDayCashRegister getFullDayCashRegister(CashRegister cashRegister,List<FullDayCashRegister> fullDayCashRegisters){
		Assert.notNull(cashRegister, "null parameter is not required");
		for(FullDayCashRegister dayCashRegister : fullDayCashRegisters){
			if(dayCashRegister.getCashRegister().getCashRegisterKey().equals(cashRegister.getCashRegisterKey())) return dayCashRegister;
		}
		return null;
	}
}
